package com.li.service.impl;

import com.li.mapper.ReportMapper;
import com.li.pojo.dto.salesStatisticDTO;
import com.li.pojo.po.Dept;
import com.li.pojo.po.IndexBase;
import com.li.pojo.po.IndexToday;
import com.li.pojo.po.User;
import com.li.pojo.vo.salesStatisticVO;
import com.li.service.ClueService;
import com.li.service.DeptService;
import com.li.service.ReportService;
import com.li.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class ReportServiceImpl implements ReportService {
    @Autowired
    private ReportMapper reportMapper;
    @Autowired
    private DeptService deptService;
    @Autowired
    private UserService userService;
    @Autowired
    private ClueService clueService;

    @Override
    public IndexToday index(String beginCreateTime, String endCreateTime) {
        IndexToday indexToday = new IndexToday();
        //基础数据
        indexToday.setBusinessNum(reportMapper.getBusinessNum(beginCreateTime, endCreateTime));
        indexToday.setContractNum(reportMapper.getContractNum(beginCreateTime, endCreateTime));
        indexToday.setCluesNum(reportMapper.getCluesNum(beginCreateTime, endCreateTime));
        indexToday.setSalesAmount(reportMapper.getSalesAmount(beginCreateTime, endCreateTime));
        //今日数据
        indexToday.setTodayBusinessNum(reportMapper.getBusinessNum(beginCreateTime, endCreateTime));
        indexToday.setTodayContractNum(reportMapper.getContractNum(beginCreateTime, endCreateTime));
        indexToday.setTodayCluesNum(reportMapper.getCluesNum(beginCreateTime, endCreateTime));
        indexToday.setTodaySalesAmount(reportMapper.getSalesAmount(beginCreateTime, endCreateTime));
        //待办事项
        indexToday.setToallocatedBusinessNum(reportMapper.getToallocatedBusinessNum(beginCreateTime, endCreateTime));
        indexToday.setTofollowedBusinessNum(reportMapper.getTofollowedBusinessNum(beginCreateTime, endCreateTime));
        indexToday.setToallocatedCluesNum(reportMapper.getToallocatedCluesNum(beginCreateTime, endCreateTime));
        indexToday.setTofollowedCluesNum(reportMapper.getTofollowedCluesNum(beginCreateTime, endCreateTime));
        return indexToday;
    }

    @Override
    public List<salesStatisticVO> salesStatistic(salesStatisticDTO salesStatisticDTO) {

        ArrayList<com.li.pojo.vo.salesStatisticVO> statisticVOS = new ArrayList<>();
        List<Integer> deptId = reportMapper.findAll();

        for (Integer id : deptId) {
            log.info("id:{}",id);
            List<User> users = reportMapper.getUserList(id);
            log.info("users:{}",users);
            for (User user : users) {
                log.info("user:{}",user);
                salesStatisticVO salesStatisticVO = new salesStatisticVO();
                //负责人
                salesStatisticVO.setCreate_by(user.getNickName());
                //部门
                String deptName = reportMapper.getDeptName(user.getDeptId());
                salesStatisticVO.setDeptName(deptName);
                //转换个数
                Integer clueNo = reportMapper.getClueNo(user.getNickName(),salesStatisticDTO.getBeginCreateTime(),salesStatisticDTO.getEndCreateTime());
                salesStatisticVO.setNum(clueNo);
                //转换率
                Double clueCount;
                Integer Count = reportMapper.getClueCount(user.getNickName(),salesStatisticDTO.getBeginCreateTime(),salesStatisticDTO.getEndCreateTime());
                log.info("count:{}",Count);
                if (Count == 0){
                    salesStatisticVO.setRadio(0.0);
                }else {
                    clueCount = (double) clueNo / Count * 100;
                    DecimalFormat decimalFormat = new DecimalFormat("#.##");
                    String format = decimalFormat.format(clueCount);
                    salesStatisticVO.setRadio(Double.valueOf(format));
                }
                log.info("salesStatisticVO:{}",salesStatisticVO);
                statisticVOS.add(salesStatisticVO);
                log.info("statisticVOS:{}",statisticVOS);

            }

        }
        return statisticVOS;
    }



    @Override
    public List<salesStatisticVO> businessChange(salesStatisticDTO salesStatisticDTO) {

        List<salesStatisticVO> statisticVOS = new ArrayList<>();
        List<Integer> deptId = reportMapper.findAll();

        for (Integer id : deptId) {
            log.info("id:{}",id);
            List<User> users = reportMapper.getUserList(id);
            log.info("users:{}",users);
            for (User user : users) {
                log.info("user:{}",user);
                salesStatisticVO salesStatisticVO = new salesStatisticVO();
                //负责人
                salesStatisticVO.setCreate_by(user.getNickName());
                //部门
                String deptName = reportMapper.getDeptName(user.getDeptId());
                salesStatisticVO.setDeptName(deptName);
                //转换个数
                Integer businessNo = reportMapper.getBusinessNo(user.getNickName(),salesStatisticDTO.getBeginCreateTime(),salesStatisticDTO.getEndCreateTime());
                salesStatisticVO.setNum(businessNo);
                //转换率
                Double businessCount;
                Integer Count = reportMapper.getBusinessCount(user.getNickName(),salesStatisticDTO.getBeginCreateTime(),salesStatisticDTO.getEndCreateTime());
                log.info("count:{}",Count);
                if (Count == 0){
                    salesStatisticVO.setRadio(0.0);
                }else {
                    businessCount = (double) businessNo / Count * 100;
                    DecimalFormat decimalFormat = new DecimalFormat("#.##");
                    String format = decimalFormat.format(businessCount);
                    salesStatisticVO.setRadio(Double.valueOf(format));
                }
                log.info("salesStatisticVO:{}",salesStatisticVO);
                statisticVOS.add(salesStatisticVO);
                log.info("statisticVOS:{}",statisticVOS);

            }

        }

        return statisticVOS;
    }
}
